package com.sunricher.telinkblemeshlib;

import android.util.Log;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class MeshEntertainmentManager {

    private int index = 0;
    private Timer timer;
    private boolean isStarted = false;
    private ArrayList<MeshEntertainmentAction> actions = new ArrayList<>();

    private MeshEntertainmentManager() {

    }

    public static MeshEntertainmentManager getInstance() {
        return MeshEntertainmentManager.SingletonHolder.instance;
    }

    public void start(List<MeshEntertainmentAction> actions) {

        this.actions = new ArrayList<>(actions);
        this.index = 0;
        cancelTimer();
        if (actions.size() > 0) {
            isStarted = true;
            int delay = actions.get(0).getDelay();
            timer = new Timer();
            timer.schedule(new TimerTask() {
                @Override
                public void run() {
                    execute();
                }
            }, delay * 1000L);
        }
    }

    private void execute() {
        if (actions.size() > 0) {
            isStarted = true;
            MeshEntertainmentAction action = actions.get(index);
            int count = sendAction(action);
            int newIndex = index + 1;
            if (newIndex >= actions.size()) {
                newIndex = 0;
            }
            index = newIndex;
            MeshEntertainmentAction newAction = actions.get(newIndex);
            int delayMs = newAction.getDelay() * 1000 + count * (int) MeshManager.getInstance().sendingTimeInterval;
            cancelTimer();
            timer = new Timer();
            timer.schedule(new TimerTask() {
                @Override
                public void run() {
                    execute();
                }
            }, delayMs);
        } else {
            isStarted = false;
        }
    }

    public void stop() {
        cancelTimer();
        this.isStarted = false;
        this.actions = new ArrayList<>();
    }

    private int sendAction(MeshEntertainmentAction action) {

        Log.i("MeshEntertainmentManager", "Send action");
        int count = 0;

        if (action.getOn() != null) {
            boolean isOn = action.getOn();
            MeshCommand cmd = MeshCommand.turnOnOff(action.getTarget(), isOn);
            MeshManager.getInstance().send(cmd);
            count += 1;
        }

        if (action.getRgb() != null) {
            int rgb = action.getRgb();
            int red = (rgb >> 16) & 0xFF;
            int green = (rgb >> 8) & 0xFF;
            int blue = rgb & 0xFF;
            MeshCommand cmd = MeshCommand.setRgb(action.getTarget(), red, green, blue);
            MeshManager.getInstance().send(cmd);
            count += 1;
        }

        if (action.getColorTemperature() != null) {
            int cct = action.getColorTemperature();
            MeshCommand cmd = MeshCommand.setColorTemperature(action.getTarget(), cct);
            MeshManager.getInstance().send(cmd);
            count += 1;
        }

        if (action.getWhite() != null) {
            int white = action.getWhite();
            MeshCommand cmd = MeshCommand.setWhitePercentage(action.getTarget(), white);
            MeshManager.getInstance().send(cmd);
            count += 1;
        }

        if (action.getBrightness() != null) {
            int brightness = action.getBrightness();
            MeshCommand cmd = MeshCommand.setBrightness(action.getTarget(), brightness);
            MeshManager.getInstance().send(cmd);
            count += 1;
        }
        return count;
    }

    public int getIndex() {
        return index;
    }

    public boolean isStarted() {
        return isStarted;
    }

    private static final class SingletonHolder {
        public static final MeshEntertainmentManager instance = new MeshEntertainmentManager();
    }

    private void cancelTimer() {
        if (timer != null) {
            timer.cancel();
        }
        timer = null;
    }
}
